{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "common-dayton",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n",
      "UTF-8\n",
      "<script>window.location.href='https://passport.jd.com/new/login.aspx?ReturnUrl=http%3A%2F%2Fitem.jd.\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "r = requests.get(\"https://item.jd.com/2967969.html\")\n",
    "print(r.status_code)\n",
    "print(r.encoding)\n",
    "print(r.text[:100])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "historic-arena",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "503\n",
      "ISO-8859-1\n",
      "<!DOCTYPE html>\n",
      "<!--[if lt IE 7]> <html lang=\"zh-CN\" class=\"a-no-js a-lt-ie9 a-lt-ie8 a-lt-ie7\"> <![endif]-->\n",
      "<!--[if IE 7]>    <html lang=\"zh-CN\" class=\"a-no-js a-lt-ie9 a-lt-ie8\"> <![endif]-->\n",
      "<!--[if IE 8]>    <html lang=\"zh-CN\" class=\"a-no-js a-lt-ie9\"> <![endif]-->\n",
      "<!--[if gt IE 8]><!-->\n",
      "<html class=\"a-no-js\" lang=\"zh-CN\"><!--<![endif]--><head>\n",
      "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">\n",
      "<meta charset=\"utf-8\">\n",
      "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\">\n",
      "<title dir=\"ltr\">Amazon.cn</title>\n",
      "<meta name=\"viewport\" content=\"width=device-width\">\n",
      "<link rel=\"stylesheet\" href=\"https://images-na.ssl-images-amazon.com/images/G/01/AUIClients/AmazonUI-3c913031596ca78a3768f4e934b1cc02ce238101.secure.min._V1_.css\">\n",
      "<script>\n",
      "\n",
      "if (true === true) {\n",
      "    var ue_t0 = (+ new Date()),\n",
      "        ue_csm = window,\n",
      "        ue = { t0: ue_t0, d: function() { return (+new Date() - ue_t0); } },\n",
      "        ue_furl = \"fls-cn.amazon.cn\",\n",
      "        ue_mid = \"AAHKV2X7AFYLW\",\n",
      "        ue_sid = (document.cookie.match(/session-id=([0-9-]+)/) || [])[1],\n",
      "        ue_sn = \"opfcaptcha.amazon.cn\",\n",
      "        ue_id = 'F95PJZWQBR5TDJN2ZNZW';\n",
      "}\n",
      "</script>\n",
      "</head>\n",
      "<body>\n",
      "\n",
      "<!--\n",
      "        To discuss automated access to Amazon data please contact api-services-support@amazon.com.\n",
      "        For information about migrating to our APIs refer to our Marketplace APIs at https://developer.amazonservices.com.cn/index.html/ref=rm_c_sv, or our Product Advertising API at https://associates.amazon.cn/gp/advertising/api/detail/main.html/ref=rm_c_ac for advertising use cases.\n",
      "-->\n",
      "\n",
      "<!--\n",
      "Correios.DoNotSend\n",
      "-->\n",
      "\n",
      "<div class=\"a-container a-padding-double-large\" style=\"min-width:350px;padding:44px 0 !important\">\n",
      "\n",
      "    <div class=\"a-row a-spacing-double-large\" style=\"width: 350px; margin: 0 auto\">\n",
      "\n",
      "        <div class=\"a-row a-spacing-medium a-text-center\"><i class=\"a-icon a-logo\"></i></div>\n",
      "\n",
      "        <div class=\"a-box a-alert a-alert-info a-spacing-base\">\n",
      "            <div class=\"a-box-inner\">\n",
      "                <i class=\"a-icon a-icon-alert\"></i>\n",
      "                <h4>请输入您在下方看到的字符</h4>\n",
      "                <p class=\"a-last\">抱歉，我们只是想确认一下当前访问者并非自动程序。为了达到最佳效果，请确保您浏览器上的 Cookie 已启用。</p>\n",
      "                </div>\n",
      "            </div>\n",
      "\n",
      "            <div class=\"a-section\">\n",
      "\n",
      "                <div class=\"a-box a-color-offset-background\">\n",
      "                    <div class=\"a-box-inner a-padding-extra-large\">\n",
      "\n",
      "                        <form method=\"get\" action=\"/errors/validateCaptcha\" name=\"\">\n",
      "                            <input type=hidden name=\"amzn\" value=\"KFvtyjhgK+PET/bD09AI6w==\" /><input type=hidden name=\"amzn-r\" value=\"&#047;gp&#047;product&#047;B01M8L5Z3Y\" />\n",
      "                            <div class=\"a-row a-spacing-large\">\n",
      "                                <div class=\"a-box\">\n",
      "                                    <div class=\"a-box-inner\">\n",
      "                                        <h4>请输入您在这个图片中看到的字符：</h4>\n",
      "                                        <div class=\"a-row a-text-center\">\n",
      "                                            <img src=\"https://images-na.ssl-images-amazon.com/captcha/xzqdsmvh/Captcha_uzujlctqtq.jpg\">\n",
      "                                        </div>\n",
      "                                        <div class=\"a-row a-spacing-base\">\n",
      "                                            <div class=\"a-row\">\n",
      "                                                <div class=\"a-column a-span6\">\n",
      "                                                    <label for=\"captchacharacters\">输入字符</label>\n",
      "                                                </div>\n",
      "                                                <div class=\"a-column a-span6 a-span-last a-text-right\">\n",
      "                                                    <a onclick=\"window.location.reload()\">换一张图</a>\n",
      "                                                </div>\n",
      "                                            </div>\n",
      "                                            <input autocomplete=\"off\" spellcheck=\"false\" id=\"captchacharacters\" name=\"field-keywords\" class=\"a-span12\" autocapitalize=\"off\" autocorrect=\"off\" type=\"text\">\n",
      "                                        </div>\n",
      "                                    </div>\n",
      "                                </div>\n",
      "                            </div>\n",
      "\n",
      "                            <div class=\"a-section a-spacing-extra-large\">\n",
      "\n",
      "                                <div class=\"a-row\">\n",
      "                                    <span class=\"a-button a-button-primary a-span12\">\n",
      "                                        <span class=\"a-button-inner\">\n",
      "                                            <button type=\"submit\" class=\"a-button-text\">继续购物</button>\n",
      "                                        </span>\n",
      "                                    </span>\n",
      "                                </div>\n",
      "\n",
      "                            </div>\n",
      "                        </form>\n",
      "\n",
      "                    </div>\n",
      "                </div>\n",
      "\n",
      "            </div>\n",
      "\n",
      "        </div>\n",
      "\n",
      "        <div class=\"a-divider a-divider-section\"><div class=\"a-divider-inner\"></div></div>\n",
      "\n",
      "        <div class=\"a-text-center a-spacing-small a-size-mini\">\n",
      "            <a href=\"https://www.amazon.cn/gp/help/customer/display.html/ref=footer_claim?ie=UTF8&nodeId=200347160\">使用条件</a>\n",
      "            <span class=\"a-letter-space\"></span>\n",
      "            <span class=\"a-letter-space\"></span>\n",
      "            <span class=\"a-letter-space\"></span>\n",
      "            <span class=\"a-letter-space\"></span>\n",
      "            <a href=\"https://www.amazon.cn/gp/help/customer/display.html/ref=footer_privacy?ie=UTF8&nodeId=200347130\">隐私声明</a>\n",
      "        </div>\n",
      "\n",
      "        <div class=\"a-text-center a-size-mini a-color-secondary\">\n",
      "          &copy; 1996-2015, Amazon.com, Inc. or its affiliates\n",
      "          <script>\n",
      "           if (true === true) {\n",
      "             document.write('<img src=\"https://fls-cn.amaz'+'on.cn/'+'1/oc-csi/1/OP/requestId=F95PJZWQBR5TDJN2ZNZW&js=1\" />');\n",
      "           };\n",
      "          </script>\n",
      "          <noscript>\n",
      "            <img src=\"https://fls-cn.amazon.cn/1/oc-csi/1/OP/requestId=F95PJZWQBR5TDJN2ZNZW&js=0\" />\n",
      "          </noscript>\n",
      "        </div>\n",
      "    </div>\n",
      "    <script>\n",
      "    if (true === true) {\n",
      "        var head = document.getElementsByTagName('head')[0],\n",
      "            prefix = \"https://images-cn.ssl-images-amazon.com/images/G/01/csminstrumentation/\",\n",
      "            elem = document.createElement(\"script\");\n",
      "        elem.src = prefix + \"csm-captcha-instrumentation.min.js\";\n",
      "        head.appendChild(elem);\n",
      "\n",
      "        elem = document.createElement(\"script\");\n",
      "        elem.src = prefix + \"rd-script-6d68177fa6061598e9509dc4b5bdd08d.js\";\n",
      "        head.appendChild(elem);\n",
      "    }\n",
      "    </script>\n",
      "</body></html>\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "r = requests.get(\"https://www.amazon.cn/gp/product/B01M8L5Z3Y\")\n",
    "print(r.status_code)\n",
    "print(r.encoding)\n",
    "r.encoding = r.apparent_encoding\n",
    "print(r.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "amended-melissa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n",
      "http://www.baidu.com/s?wd=python\n",
      "663466\n"
     ]
    }
   ],
   "source": [
    "keyword = 'python'\n",
    "try:\n",
    "    kv = {'wd':keyword}\n",
    "    r = requests.get('http://www.baidu.com/s',params=kv)\n",
    "    print(r.status_code)\n",
    "    print(r.request.url)\n",
    "    r.raise_for_status\n",
    "    print(len(r.text))\n",
    "except:\n",
    "    print('爬取失败')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "conservative-quantum",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    }
   ],
   "source": [
    "path = 'D://scraping//export//abc.jpg'\n",
    "url = 'https://dss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2771978851,2906984932&fm=26&gp=0.jpg'\n",
    "r = requests.get(url)\n",
    "print(r.status_code)\n",
    "with open(path, 'wb') as f:\n",
    "    f.write(r.content)\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "continued-stream",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "文件保存成功\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "url = 'https://dss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1603365312,3218205429&fm=26&gp=0.jpg'\n",
    "root = 'D://scraping//export1//'\n",
    "path = root + url.split('=')[-1]\n",
    "try:\n",
    "    if not os.path.exists(root):\n",
    "        os.mkdir(root)\n",
    "    if not os.path.exists(path):\n",
    "        r = requests.get(url)\n",
    "        with open(path, 'wb') as f:\n",
    "            f.write(r.content)\n",
    "            f.close()\n",
    "            print('文件保存成功')\n",
    "    else:\n",
    "        print('文件已存在')\n",
    "except:\n",
    "    print('爬取失败')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
